﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;
using ArtOfTest.WebAii.Silverlight.UI;
using ArtOfTest.WebAii.ObjectModel;
using ArtOfTest.WebAii.Controls.HtmlControls;

namespace SM2Input
{
    public class Tradition
    {
        #region Button

        public static void ClickButtonBYID(string stringButtonID)
        {
            try
            {
                HtmlInputSubmit btn = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlInputSubmit>(stringButtonID);
                btn.Click();
                Utilities.GlobalObject.MANAGER.ActiveBrowser.WaitUntilReady();
                System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
                Utilities.GlobalObject.LOGGER.log("Clicked Button [" + stringButtonID + "]");
            }
            catch (Exception ex)
            {
                GlobalObject.LOGGER.log("Failed to interact with Tradition[" + stringButtonID + "]");
                throw ex;
            }
        }

        public static void ClickButtonBYNAME(string strindButtonName)
        {
            try
            {
                Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ByName<HtmlInputSubmit>(strindButtonName).Click();
                Utilities.GlobalObject.MANAGER.ActiveBrowser.WaitUntilReady();
                System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
                Utilities.GlobalObject.LOGGER.log("Clicked Button [" + strindButtonName + "]");
            }
            catch (Exception ex)
            {
                GlobalObject.LOGGER.log("Failed to interact with Tradition[" + strindButtonName + "]");
                throw ex;
            }
        }

        public static void ClickButton(HtmlInputSubmit btn)
        {
            try
            {
                btn.Click();
                Utilities.GlobalObject.MANAGER.ActiveBrowser.WaitUntilReady();
                System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
                Utilities.GlobalObject.LOGGER.log("Clicked Button [" + btn.ID + "]");
            }
            catch (Exception ex)
            {
                GlobalObject.LOGGER.log("Failed to interact with Tradition[" + btn.ID + "]");
                throw ex;
            }
        }

        #endregion

        #region TextField

        public static void TypeTextIntoTextField(HtmlInputText inputTextField, string strText)
        {
            try
            {
                inputTextField.Text = strText;
                Utilities.GlobalObject.LOGGER.log("Set Text = " + strText + " into TextField [" + inputTextField.ID + "]");
            }
            catch (Exception ex)
            {
                GlobalObject.LOGGER.log("Failed to interact with Tradition[" + inputTextField.ID + "]");
                throw ex;
            }
        }

        public static void TypeTextIntoTextFieldWithHint(HtmlInputText inputTextField, string strText)
        {
            try
            {
                inputTextField.MouseClick(ArtOfTest.WebAii.Core.MouseClickType.LeftDoubleClick);
                inputTextField.Text = strText;
                Utilities.GlobalObject.LOGGER.log("Set Text = " + strText + " into TextField [" + inputTextField.ID + "]");
            }
            catch (Exception ex)
            {
                GlobalObject.LOGGER.log("Failed to interact with Tradition[" + inputTextField.ID + "]");
                throw ex;
            }
        }

        public static void TypeTextFieldBYID(string stringTextFieldID, string stringInputText)
        {
            try
            {
                HtmlInputText inputTextField = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlInputText>(stringTextFieldID);
                inputTextField.Text = stringInputText;
                inputTextField.Click();
            }
            catch (Exception ex)
            {
                GlobalObject.LOGGER.log("Failed to interact with Tradition[" + stringTextFieldID + "]");
                throw ex;
            }
        }

        public static void TypeTextFieldBYNAME(string stringTextFieldNAME, string stringInputText)
        {
            try
            {
                Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ByName<HtmlInputText>(stringTextFieldNAME).Text = stringInputText;
            }
            catch (Exception ex)
            {
                GlobalObject.LOGGER.log("Failed to interact with Tradition[" + stringTextFieldNAME + "]");
                throw ex;
            }
        }

        public static void TypePasswordBYID(string stringPasswordID, string stringInputText)
        {
            try
            {
                HtmlInputPassword pwd = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlInputPassword>(stringPasswordID);
                pwd.Text = stringInputText;
            }
            catch (Exception ex)
            {
                HtmlInputPassword pwd = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlInputPassword>(stringPasswordID);
                pwd.Value = stringInputText;
                GlobalObject.LOGGER.log("Failed to interact with Tradition[" + stringPasswordID + "]");
                throw ex;
            }
        }

        #endregion

        #region Anchor

        public static void ClickAnchorBYID(string stringLinkID)
        {
            try
            {
                Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlAnchor>(stringLinkID).Click();
            }
            catch (Exception ex)
            {
                GlobalObject.LOGGER.log("Failed to interact with Tradition[" + stringLinkID + "]");
                throw ex;
            }
        }

        public static void ClickAnchor(HtmlAnchor htmlAnchor)
        {
            try
            {
                htmlAnchor.Click();
                Utilities.GlobalObject.MANAGER.ActiveBrowser.WaitUntilReady();
                System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
                Utilities.GlobalObject.LOGGER.log("Clicked Anchor [" + htmlAnchor.ID + "]");
            }
            catch (Exception ex)
            {
                GlobalObject.LOGGER.log("Failed to interact with Tradition[" + htmlAnchor.ID + "]");
                throw ex;
            }
        }

        #endregion

        #region Checkbox

        public static void SelectCheckbox(HtmlInputCheckBox htmlInputCheckbox)
        {
            try
            {
                htmlInputCheckbox.Click();
                Utilities.GlobalObject.MANAGER.ActiveBrowser.WaitUntilReady();
                System.Threading.Thread.Sleep(1000);
                Utilities.GlobalObject.LOGGER.log("Clicked Anchor [" + htmlInputCheckbox.ID + "]");
            }
            catch (Exception ex)
            {
                GlobalObject.LOGGER.log("Failed to interact with Tradition[" + htmlInputCheckbox.ID + "]");
                throw ex;
            }
        }

        #endregion

        #region Radio

        public static void SelectRadio(HtmlInputRadioButton htmlInputRadioButton)
        {
            try
            {
                htmlInputRadioButton.Click();
                Utilities.GlobalObject.MANAGER.ActiveBrowser.WaitUntilReady();
                System.Threading.Thread.Sleep(1000);
                Utilities.GlobalObject.LOGGER.log("Clicked Anchor [" + htmlInputRadioButton.ID + "]");
            }
            catch (Exception ex)
            {
                GlobalObject.LOGGER.log("Failed to interact with Tradition[" + htmlInputRadioButton.ID + "]");
                throw ex;
            }
        }

        #endregion

        #region SelectList

        public static void SelectItemSelectList(HtmlSelect htmlSelect, string itemName)
        {
            try
            {
                string strSelectedOption = htmlSelect.SelectedOption.Text.Trim();
                if (!strSelectedOption.Equals(itemName))
                {
                    int intCountOptions = htmlSelect.Options.Count;
                    for (int i = 0; i < intCountOptions; i++)
                    {
                        string strBeingCheckedOption = htmlSelect.Options[i].Text.Trim();
                        if (strBeingCheckedOption.Equals(itemName))
                        {
                            htmlSelect.SelectByText(itemName);
                            System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                            break;
                        }
                    }
                }
            }
            catch 
            {
                int intCountOptions = htmlSelect.Options.Count;
                for (int i = 0; i < intCountOptions; i++)
                {
                    string strBeingCheckedOption = htmlSelect.Options[i].Text.Trim();
                    if (strBeingCheckedOption.Equals(itemName))
                    {
                        htmlSelect.SelectByText(itemName);
                        System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                        break;
                    }
                }
            }
        }

        #endregion

        #region Textarea

        public static void TypeTextIntoTextAreaWithHint(HtmlTextArea textArea, string strText)
        {
            try
            {
                textArea.MouseClick(ArtOfTest.WebAii.Core.MouseClickType.LeftDoubleClick);
                textArea.Text = strText;
                Utilities.GlobalObject.LOGGER.log("Set Text = " + strText + " into TextField [" + textArea.ID + "]");
            }
            catch (Exception ex)
            {
                GlobalObject.LOGGER.log("Failed to interact with Tradition[" + textArea.ID + "]");
                throw ex;
            }
        }

        #endregion
    }
}
